# Packages ----------------------------------------------------------------

if(!require(pacman)){install.packages("pacman")}
pacman::p_load(tidyverse, haven, MetBrewer)

# LINE Plot ---------------------------------------------------------------

## Rearrange the data

df_plot <- read_dta("data/daily_adoptions_panel_public.dta") 

## Plot the data
df_plot %>% 
  ggplot(
    aes(
      x = as.numeric(days), 
      y = as.numeric(larc_mean),
      group = haven::as_factor(view),
      color = haven::as_factor(view)
    )
  ) +
  geom_vline(
    xintercept = 100,
    linetype = "dashed"
  ) + 
  geom_line(
    size = 0.5
  ) +
  geom_ribbon(
    aes(
      ymin = lb, 
      ymax = ub
    ), 
    alpha = 0.05,
    linetype = 3,
    size = 0.25
  ) +
  scale_color_manual(values = met.brewer("Egypt", 3)) +
  scale_x_continuous(limits = c(0, 180), breaks = seq(0,180,30)) +
  scale_y_continuous(labels = scales::percent, breaks = seq(0,.7,.1), limits = c(0,.7)) +
  facet_wrap(~ haven::as_factor(discount)) +
  guides(fill = guide_legend(nrow = 1, reverse = TRUE)) +
  labs(
    color = "",
    x = "Days since first consultation",
    y = "Fraction using a LARC"
  ) +
  theme_bw() +
  theme(
    legend.position = "bottom",
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank()
  )

ggsave("output/figures/FigS5-pilot_larc_vd.png", dpi = 320, scale = 0.8, height = 16, width = 24, units = "cm")
